﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    public class ContactDAO : DAOBase, IContactDAO
    {
        #region IContactDAO Members

        public CollectionFeedback<Contact> GetCollection(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Contact> {IsSuccessful = false, Data = new List<Contact>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var contact = new Contact();
                        contact.ContactID = Convert.ToInt32(reader["ContactID"]);
                        contact.FullName = Convert.ToString(reader["FullName"]);
                        contact.MobilePhone = Convert.ToString(reader["MobilePhone"]);

                        if (reader["PartnerID"].GetType().Equals(typeof (int)))
                        {
                            contact.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                        }
                        else
                        {
                            contact.PartnerID = 0;
                        }
                        contact.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                        feedback.Data.Add(contact);
                    }
                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                }

                //Log.Info("GetCollectionContact: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Contact> GetCollectionType(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Contact> {IsSuccessful = false, Data = new List<Contact>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var contact = new Contact();
                        contact.ContactID = Convert.ToInt32(reader["ContactID"]);
                        contact.FullName = Convert.ToString(reader["FullName"]);
                        contact.MobilePhone = Convert.ToString(reader["MobilePhone"]);

                        if (reader["PartnerID"].GetType().Equals(typeof (int)))
                        {
                            contact.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                        }
                        else
                        {
                            contact.PartnerID = 0;
                        }
                        contact.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                        feedback.Data.Add(contact);
                    }
                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                }

                //Log.Info("GetCollectionContact: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<Contact> GetSingle(int objectID)
        {
            var feedback = new SingleFeedback<Contact> {IsSuccessful = false, Data = new Contact()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("GetSingleContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@ContactID", objectID);

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var contact = new Contact();
                        contact.ContactID = Convert.ToInt32(reader["ContactID"]);
                        contact.FullName = Convert.ToString(reader["FullName"]);
                        contact.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                        contact.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                        contact.IsDelete = Convert.ToBoolean(reader["IsDelete"]);


                        feedback.Data = contact;

                        break;
                    }
                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                }

                Log.Info("GetSingleContact: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<int> Insert(Contact data)
        {
            var feedback = new SingleFeedback<int> {IsSuccessful = false, Data = 0};
            try
            {
                OpenConnection();

                var command = new SqlCommand("InsertContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@FullName", data.FullName);
                command.Parameters.AddWithValue("@MobilePhone", data.MobilePhone);
                command.Parameters.AddWithValue("@PartnerID", data.PartnerID);

                feedback.Data = command.ExecuteNonQuery();

                if (feedback.Data >= 1)
                {
                    feedback.Message = InventoryConstants.Messages.InsertSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                }

                Log.Info("InsertContact: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.InsertFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.InsertFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Update(Contact data)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                OpenConnection();

                var command = new SqlCommand("UpdateContact", imsConnection);
                var command2 = new SqlCommand("GetSingleContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command2.CommandType = CommandType.StoredProcedure;
                command2.Parameters.AddWithValue("@ContactID", data.ContactID);

                var da = new SqlDataAdapter(command2);
                var dt = new DataTable("GetContactByIDTable");
                da.Fill(dt);

                if (data.ContactID != 0)
                {
                    DataRow dr = dt.Rows[0];
                    command.Parameters.AddWithValue("@ContactID", data.ContactID);
                    command.Parameters.AddWithValue("@FullName",
                                                    string.IsNullOrEmpty(data.FullName)
                                                        ? Convert.ToString(dr["FullName"])
                                                        : data.FullName);
                    command.Parameters.AddWithValue("@MobilePhone",
                                                    string.IsNullOrEmpty(data.MobilePhone)
                                                        ? Convert.ToString(dr["MobilePhone"])
                                                        : data.MobilePhone);
                    command.Parameters.AddWithValue("@PartnerID",
                                                    data.PartnerID.Equals(dr["PartnerID"])
                                                        ? Convert.ToInt32(dr["PartnerID"])
                                                        : data.PartnerID);

                    int rowAffect = command.ExecuteNonQuery();

                    if (rowAffect < 1)
                    {
                        feedback.Message = InventoryConstants.Messages.UpdateFailed;
                    }
                    else
                    {
                        feedback.Message = InventoryConstants.Messages.UpdateSuccessfully;
                        feedback.IsSuccessful = true;
                    }

                    Log.Info("UpdateEmployee: {0}, {1}, {2}", data.ContactID, feedback.Message, DateTime.Now.ToString());
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                }
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.UpdateFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.UpdateFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Delete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};

            try
            {
                OpenConnection();

                var command = new SqlCommand("DeleteContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@ContactID", objID);

                int rowAffect = command.ExecuteNonQuery();

                if (rowAffect >= 1)
                {
                    feedback.Message = InventoryConstants.Messages.DeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                
                Log.Info("DeleteContact: {0}, {1}, {2}", objID, feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.DeleteFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.DeleteFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Contact> Search(Contact criteria)
        {
            throw new NotImplementedException();
        }

        public Feedback UndoDelete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};

            try
            {
                OpenConnection();

                var command = new SqlCommand("UndoDeletedContact", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@ContactID", objID);

                int rowAffect = command.ExecuteNonQuery();

                if (rowAffect >= 1)
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }

                Log.Info("UndoDeletedContact: {0}, {1}, {2}", objID, feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        #endregion
    }
}